﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WF_QLThuVien.Utilities;
using WF_QLThuVien.Entities.DauSach;
using WF_QLThuVien.BusinessLayer.DauSach;
using WF_QLThuVien.DataLayer.DauSach;

namespace WF_QLThuVien.PresentationLayer
{
    public partial class FormTimKiemSach : Form
    {
        CBLL_DauSach bllDauSach = new CBLL_DauSach();
        CDAL_DauSach dalSauSach = new CDAL_DauSach();

        public delegate void SentData(List<CDauSach> lstDauSach, bool rdbSach, string SachID);
        public event SentData sdt;

        public delegate void CloseF(bool flag);
        public event CloseF dongForm;

        public FormTimKiemSach()
        {
            InitializeComponent();
            sdt += new SentData(FormTimKiemSach_sdt);
            dongForm += new CloseF(FormTimKiemSach_dongForm);
        }

        void FormTimKiemSach_dongForm(bool flag)
        {

        }

        void FormTimKiemSach_sdt(List<CDauSach> lstDauSach, bool rdbSach, string SachID)
        {

        }

        private void rdbTimChinhXac_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTimChinhXac.Checked)
            {
                gbTimChinhXac.Enabled = true;
                gbTimGanDung.Enabled = false;

                txtTimChinhXac.Focus();
            }
            else
            {
                gbTimChinhXac.Enabled = false;
                gbTimGanDung.Enabled = true;


                chkTrangThai_CheckedChanged(sender, e);
                chkTheLoai_CheckedChanged(sender, e);
                chkTenSach_CheckedChanged(sender, e);
                chkTacGia_CheckedChanged(sender, e);
                chkNhaXB_CheckedChanged(sender, e);
                chkNgonNgu_CheckedChanged(sender, e);
                chkNgayNhap_CheckedChanged(sender, e);
                chkNamXB_CheckedChanged(sender, e);


            }
        }

        private void FormTimKiemSach_Load(object sender, EventArgs e)
        {
            rdbTimChinhXac_CheckedChanged(sender, e);            

            CPhatSinhMa.ChuoiComboBox(CVariable.TRANG_THAI_SACH, cbbTrangThai);
            cbbTrangThai.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbTrangThai.SelectedIndex = 0;

            CPhatSinhMa.ChuoiComboBox(CVariable.NGON_NGU, cbbNgonNgu);
            cbbNgonNgu.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbNgonNgu.SelectedIndex = 0;

            CPhatSinhMa.ChuoiComboBox(CVariable.NAM_XUAT_BAN, cbbNamXB);
            cbbNamXB.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbNamXB.SelectedIndex = 0;

            CPhatSinhMa.ChuoiComboBox(CVariable.NHA_XUAT_BAN, cbbNhaXB);
            cbbNhaXB.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbNhaXB.SelectedIndex = 0;

            CPhatSinhMa.ChuoiComboBox(CVariable.TAC_GIA, cbbTacGia);
            cbbTacGia.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbTacGia.SelectedIndex = 0;

            CPhatSinhMa.ChuoiComboBox(CVariable.THE_LOAI, cbbTheLoai);
            cbbTheLoai.DropDownStyle = ComboBoxStyle.DropDownList;
            cbbTheLoai.SelectedIndex = 0;
        }

        private void tcxtxtMaDauSach_TextChanged(object sender, EventArgs e)
        {            
                                     
        }

        private void tgdtxtTenDauSach_TextChanged(object sender, EventArgs e)
        {           
                                        
        }

        private void chkTenSach_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTenSach.Checked)
            {
                txtTenSach.Enabled = true;
            }
            else
            {
                txtTenSach.Enabled = false;
            }
        }

        private void chkNgayNhap_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNgayNhap.Checked)
            {
                dtpNgayNhap.Enabled = true;
            }
            else
            {
                dtpNgayNhap.Enabled = false;
            }
        }

        private void chkTacGia_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTacGia.Checked)
            {
                cbbTacGia.Enabled = true;
            }
            else
            {
                cbbTacGia.Enabled = false;
            }
        }

        private void chkTheLoai_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTheLoai.Checked)
            {
                cbbTheLoai.Enabled = true;
            }
            else
            {
                cbbTheLoai.Enabled = false;
            }
        }

        private void chkNgonNgu_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNgonNgu.Checked)
            {
                cbbNgonNgu.Enabled = true;
            }
            else
            {
                cbbNgonNgu.Enabled = false;
            }
        }

        private void chkNamXB_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNamXB.Checked)
            {
                cbbNamXB.Enabled = true;
            }
            else
            {
                cbbNamXB.Enabled = false;
            }
        }

        private void chkNhaXB_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNhaXB.Checked)
            {
                cbbNhaXB.Enabled = true;
            }
            else
            {
                cbbNhaXB.Enabled = false;
            }
        }

        private void chkTrangThai_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTrangThai.Checked)
            {
                cbbTrangThai.Enabled = true;
            }
            else
            {
                cbbTrangThai.Enabled = false;
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            List<CDauSach> lstDauSach = new List<CDauSach>();
            string TimChinhXac = txtTimChinhXac.Text.ToUpper();

            if (rdbTimChinhXac.Checked)
            {
                if (!CKiemTra.IsNull(txtTimChinhXac, "Chưa nhập nội dung tìm kiếm!", timDauSachErr))
                {
                    CDauSach ds = null;
                    
                    if (rdbMaSach.Checked)
                    {
                        ds = bllDauSach.LoadDauSachBySachID(TimChinhXac);
                    }
                    else
                    {
                        ds = bllDauSach.LoadDauSachByDauSachID(TimChinhXac);
                    }

                    if (ds == null)
                    {
                        MessageBox.Show("Không tồn tại mã số này!");
                    }
                    else
                    {
                        lstDauSach.Add(ds);
                    }
                }
                else
                {
                    MessageBox.Show("Chưa nhập nội dung tìm kiếm!");
                }
            }
            else
            {
                //phần của tìm gần đúng
                List<string> columnsNames = new List<string>();
                List<string> values = new List<string>();

                if (chkTenSach.Checked)
                {
                    columnsNames.Add("TenDauSach");
                    values.Add(CXuLiChuoi.VietHoaDauMoiChu(txtTenSach.Text));
                }
                if (chkNgayNhap.Checked)
                {
                    columnsNames.Add("NgayNhap");
                    values.Add(dtpNgayNhap.Value.ToShortDateString());
                }
                if (chkTacGia.Checked)
                {
                    columnsNames.Add("TacGia");
                    values.Add(cbbTacGia.SelectedItem.ToString());
                }
                if (chkTheLoai.Checked)
                {
                    columnsNames.Add("TheLoai");
                    values.Add(cbbTheLoai.SelectedItem.ToString());
                }
                if (chkNgonNgu.Checked)
                {
                    columnsNames.Add("NgonNgu");
                    values.Add(cbbNgonNgu.SelectedItem.ToString());
                }
                if (chkNamXB.Checked)
                {
                    columnsNames.Add("NamXB");
                    values.Add(cbbNamXB.SelectedItem.ToString());
                }
                if (chkNhaXB.Checked)
                {
                    columnsNames.Add("NhaXB");
                    values.Add(cbbNhaXB.SelectedItem.ToString());
                }
                if (chkTrangThai.Checked)
                {
                    columnsNames.Add("TrangThai");
                    values.Add(cbbTrangThai.SelectedItem.ToString());
                }

                lstDauSach = dalSauSach.GetListByListColumnsNames(columnsNames, values);
                if (lstDauSach == null)
                {
                    MessageBox.Show("Không có thông tin về loại sách cần tìm");
                }                
            }
            sdt(lstDauSach, rdbMaSach.Checked, TimChinhXac);
        }

        private void FormTimKiemSach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Dispose();
            }
        }

        private void FormTimKiemSach_FormClosed(object sender, FormClosedEventArgs e)
        {
            dongForm(true);
        }
    }
}
